//
//  commentCell.m
//  Feeds2
//
//  Created by cos on 2020/11/29.
//  Copyright © 2020 cos. All rights reserved.
//

#import "commentCell.h"
#import "AFNetworking.h"
#import "Single.h"
@implementation commentCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
   self = [super initWithStyle:style        reuseIdentifier:reuseIdentifier];

   if (self) {
       label = [[MPlabel alloc] init];
       [self.contentView addSubview:label];
       
       
   }
   return self;
}

- (void)setLabel1Text:(NSString *)text1
           label2Text:(NSString *)text2
           label3Text:(NSString *)text3
           label4Text:(NSString *)text4{
    
    [label setLabelWithOrigin:CGPointMake(5, 45) mutableText:text1 mutableFont:[UIFont boldSystemFontOfSize:20] margin:0];
    if(label.frame.size.height > 60){
        [self setFrame:CGRectMake(0, 0, 100, label.frame.size.height + 20)];
    }
    
    //likebutton
    likebutton = [UIButton buttonWithType:UIButtonTypeCustom];
    if(label.frame.size.height > 60){
        likebutton.frame = CGRectMake(350, label.frame.size.height+20, 24, 24);
    }else{
        likebutton.frame = CGRectMake(350, label.frame.size.height+20, 24, 24);
    }
    
    [likebutton setImage:[UIImage imageNamed:@"like1.png"] forState:UIControlStateNormal];
    [likebutton addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:likebutton];
    //likebutton
    
    //commentbutton
    commentbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    if(label.frame.size.height > 60){
        commentbutton.frame = CGRectMake(320, label.frame.size.height+20, 24, 24);
    }else{
        commentbutton.frame = CGRectMake(320, label.frame.size.height+20, 24, 24);
    }
    
    [commentbutton setImage:[UIImage imageNamed:@"comment.png"] forState:UIControlStateNormal];
    [commentbutton addTarget:self action:@selector(BtnClick2:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:commentbutton];
    //commentbutton
    
    //likenum
    
    if(label.frame.size.height > 60){
        likeNum = [[UILabel alloc] initWithFrame:CGRectMake(300, label.frame.size.height-22, 50, 50)];
    }else{
        likeNum = [[UILabel alloc] initWithFrame:CGRectMake(300, label.frame.size.height-42, 50, 50)];
    }
    likeNum.text = text2;
    likeNum.font = [UIFont systemFontOfSize: 16.0f];
    //[self.contentView addSubview:likeNum];
    //likenum
    //date & user
    date = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 220, 20)];
    date.text = text3;
    date.font = [UIFont systemFontOfSize: 16.0f];
    username = [[UILabel alloc] initWithFrame:CGRectMake(5, 20, 200, 30)];
    username.text = text4;
    username.font = [UIFont systemFontOfSize: 20.0f];
    [self.contentView addSubview:date];
    [self.contentView addSubview:username];
    //date & user
}

-(void)BtnClick:(UIButton *)btn{
    if(likestate){
        [btn setImage:[UIImage imageNamed:@"like1.png"] forState:UIControlStateNormal];
        likestate = !likestate;
    }
    else{
        [btn setImage:[UIImage imageNamed:@"like2.png"] forState:UIControlStateNormal];
        //likestate = !likestate;
        //
        AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        NSNumber * boolNumber = [NSNumber numberWithBool:NO];
        NSNumber * boolNumber2 = [NSNumber numberWithBool:YES];

        NSDictionary *dict = @{
                               @"isContent":boolNumber,
                               @"isComment":boolNumber2
                               };
        UITableViewCell  *cell;
        cell = (UITableViewCell *)self.contentView.superview;
        UITableView *tbv;
        tbv = (UITableView *)cell.superview;
        NSString *url = @"http://172.18.178.56/api/like/";
        NSString *contentID = [Single sharedInstance].commentlist[[tbv indexPathForCell:cell].section][@"ID"];

        url = [url stringByAppendingFormat:@"%@", contentID];

        [manager POST:url parameters:dict headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            //[self.window.rootViewController viewDidLoad];
            NSLog(@"@post like success");
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        //
    }
}

-(void)BtnClick2:(UIButton *)btn{
    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    UITableViewCell  *cell;
    cell = (UITableViewCell *)self.contentView.superview;
    UITableView *tbv;
    tbv = (UITableView *)cell.superview;
    NSString *url = @"http://172.18.178.56/api/comment";
    NSString *contentID = [Single sharedInstance].commentlist[[tbv indexPathForCell:cell].section][@"ID"];
    NSString *fatherID = [Single sharedInstance].commentlist[[tbv indexPathForCell:cell].section][@"UserID"];
    NSString *userID = [Single sharedInstance].commentlist[[tbv indexPathForCell:cell].section][@"UserID"];
    NSLog(@"%@",fatherID);
    //input
    [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"评论" message:@"你想说啥" preferredStyle:UIAlertControllerStyleAlert];
    //以下方法就可以实现在提示框中输入文本；
    //在AlertView中添加一个输入框
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        textField.placeholder = @"请输入";
    }];
    //添加一个确定按钮 并获取AlertView中的第一个输入框 将其文本赋值给BUTTON的title
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSNumber * boolNumber = [NSNumber numberWithBool:YES];
        UITextField *envirnmentNameTextField = alertController.textFields.firstObject;
        
        NSString* content = [NSString stringWithFormat:@"%@",envirnmentNameTextField.text];
        NSDictionary *dict = @{
            @"contentId":contentID,@"isReply":boolNumber,@"content":content,@"fatherId":fatherID
        };
        [manager POST:url parameters:dict headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            //[self.window.rootViewController viewDidLoad];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }]];
    //添加一个取消按钮
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
    
    //present出AlertView
    [[self getCurrentViewController] presentViewController:alertController animated:YES completion:nil];

    //input

}

#pragma mark - Utils
- (UIViewController *)getCurrentViewController
{
    UIViewController *currentViewController = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if ([window subviews].count == 0) {
        return nil;
    }
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * subWindow in windows)
        {
            if (subWindow.windowLevel == UIWindowLevelNormal) {
                window = subWindow;
                break;
            }
        }
    }
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        currentViewController = nextResponder;
    } else {
        currentViewController = window.rootViewController;
    }
    return currentViewController;
}

@end
